<?php

namespace org;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Sms\V20190711\Models\SendSmsRequest;
use TencentCloud\Sms\V20190711\SmsClient;

class SendMsg
{
    /**
     * 手机号
     * @var string
     */
    private $sign_name = "";
    private $template_id = "";

    private $keyid = '';
    private $appid = ''; //腾讯云
    private $secret = '';

    public function __construct($keyid = '',$secret = '',$appid = '',$sign_name = '',$template_id = '')
    {
        if (empty($keyid) || empty($secret) || empty($sign_name) || empty($template_id)) {
            throw new \think\Exception("参数缺失!");
        }
        $this->keyid = $keyid;
        $this->secret = $secret;
        $this->appid = $appid;
        $this->sign_name = $sign_name;
        $this->template_id = $template_id;
    }

    /**
     * 阿里云发送短信
     * @param string $phone 手机号
     * @param array $param  参数数组形式
     * @throws ClientException
     */
    public function aliYun($phone = '',$param = array())
    {
        AlibabaCloud::accessKeyClient($this->keyid, $this->secret)
            ->regionId('cn-hangzhou')
            ->asDefaultClient();

        try {
            $result = AlibabaCloud::rpc()
                ->product('Dysmsapi')
                // ->scheme('https') // https | http
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->host('dysmsapi.aliyuncs.com')
                ->options([
                    'query' => [
                        'RegionId' => "cn-hangzhou",
                        'PhoneNumbers' => $phone,
                        'SignName' => $this->sign_name,
                        'TemplateCode' => $this->template_id,
                        'TemplateParam' => json_encode($param),
                    ],
                ])
                ->request();
            print_r($result->toArray());
        } catch (ClientException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        } catch (ServerException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        }

    }

    /**
     * 腾讯云发送短信
     * @param string $phone 手机号
     * @param array $param  参数数组形式
     * @return false|mixed
     */
    public function tencent($phone = '',$param = array())
    {
        try {

            $cred = new Credential($this->keyid, $this->secret);
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("sms.tencentcloudapi.com");

            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            $client = new SmsClient($cred, "", $clientProfile);

            $req = new SendSmsRequest();

            $params = array(
                "PhoneNumberSet" => array($phone),
                "TemplateParamSet" => $param,
                "TemplateID" => $this->template_id,
                "SmsSdkAppid" => $this->appid,
                "Sign" => $this->sign_name
            );
            $req->fromJsonString(json_encode($params));

            $resp = $client->SendSms($req);

            return json_decode($resp->toJsonString(),true);
        }
        catch(TencentCloudSDKException $e) {
            echo $e;
        }
        return false;
    }
}